package com.canyou.basic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canyou.api.BaseApiController;
import com.canyou.api.ResponseBO;
import com.canyou.basic.entity.sms.SmsRecordReSendDTO;
import com.canyou.basic.entity.sms.SmsSendVO;
import com.canyou.basic.entity.sms.SmsStatusEnum;
import com.canyou.basic.model.SmsSend;
import com.canyou.basic.service.SmsSendService;
import com.canyou.enums.ErrorCodeEnum;
import com.canyou.exception.CysBusinessException;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import static com.canyou.api.ResponseBO.illegal;
import static com.canyou.api.ResponseBO.ok;

/**
 * <p>
 * 短信发送记录表前端控制器
 * </p>
 *
 * @author canyou
 * @since 2020-06-28
 */
@Api(tags = "短信发送")
@RestController
@RequestMapping("/sms-send")
@Validated
public class SmsSendController extends BaseApiController {

    @Autowired
    private SmsSendService smsSendService;

    @ApiOperation(value = "分页获取短信列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", paramType = "query", value = "当前第几页",
                    dataType = "int", required = false, example = "1"),
            @ApiImplicitParam(name = "pageSize", paramType = "query", value = "每页记录数",
                    dataType = "int", required = false, example = "10"),
            @ApiImplicitParam(name = "condition", paramType = "query", value = "手机号/发送内容/接收人姓名",
                    dataType = "string", required = false, example = "")
    })
    @GetMapping("/get_page_list")
    public ResponseBO<IPage<SmsSendVO>> getPageList(@RequestParam(defaultValue = PAGE_INDEX, required = false) int pageNumber,
                                                    @RequestParam(defaultValue = PAGE_SIZE, required = false) int pageSize,
                                                    @RequestParam(required = false) String condition) {
        IPage<SmsSendVO> page = new Page<>(pageNumber, pageSize);
        IPage<SmsSendVO> pageList = smsSendService.getPageList(page, condition);
        return ok(pageList);
    }

    @ApiOperation(value = "短信发送失败的记录重新发送")
    @PostMapping("/record-resend")
    public ResponseBO recordResend(
            @ApiParam(name = "smsRecordReSendDTO", type = "SmsRecordReSendDTO", value = "短信发送失败的记录信息数据", required = true)
            @Valid
            @NotNull(message = "短信发送失败的记录信息数据不能为空")
            @RequestBody SmsRecordReSendDTO smsRecordReSendDTO) {
        SmsSend smsSendRecord = smsSendService.getById(smsRecordReSendDTO.getId());
        // 对应id的短信发送记录不存在
        if (smsSendRecord == null) {
            throw new CysBusinessException(ErrorCodeEnum.ILLEGAL, "短信发送记录不存在");
        }
        // 验证码类短信不能重发
        if (Integer.valueOf(1).equals(smsSendRecord.getSmsType())) {
            throw new CysBusinessException(ErrorCodeEnum.ILLEGAL, "验证码短信不能重发");
        }
        // 如果已经发送成功则不再需要重新发送, send_status: (1：等待回执，2：发送失败，3：发送成功)
        if (!SmsStatusEnum.FAILED.getValue().equals(smsSendRecord.getSendStatus())) {
            throw new CysBusinessException(ErrorCodeEnum.ILLEGAL, "该次短信发送记录成功, 不需要重新发送");
        }
        // 如果已经重发则不再需要重新发送
        if (Integer.valueOf(1).equals(smsSendRecord.getIsResend())) {
            throw new CysBusinessException(ErrorCodeEnum.ILLEGAL, "该次短信发送记录已经重发, 不需要重新发送");
        }
        // 重发
        boolean res = smsSendService.reSendSms(smsSendRecord);
        if (!res) {
            return illegal("重发失败, 请在发送记录查看失败原因");
        }
        return ok("重发成功");
    }

    @ApiOperation(value = "同步短信发送回执")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "短信发送记录id",
                    dataType = "int", required = true, example = "1")
    })
    @GetMapping("/get_sms_send_receipt")
    public ResponseBO getSmsSendReceipt(
            @NotNull(message = "短信发送记录id不能为空")
            @RequestParam(required = false) Long id) {
        SmsSend smsSendRecord = smsSendService.getById(id);
        // 对应id的短信发送记录不存在
        if (smsSendRecord == null) {
            throw new CysBusinessException(ErrorCodeEnum.ILLEGAL, "短信发送记录不存在");
        }
        Boolean res = smsSendService.syncSmsSendReceipt(smsSendRecord);
        if (res == null || !res) {
            return illegal("同步短信发送回执失败");
        }
        return ok("同步短信发送回执成功");
    }

}
